package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;


public class DetailsPage extends Utils {
    private static String url = "http://8.134.99.135:58080/index.html";

    public DetailsPage() {
        super(url);
    }


    //未登录状态，直接跳回登录页面
    public void noLoginDetails() {
        String currentUrl = driver.getCurrentUrl();
        Assertions.assertEquals("http://8.134.99.135:58080/sign-in.html", currentUrl);
    }


    //登录状态，可以看到完整的内容
    public void loginDetails() {
        WebElement detailsElement = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > " +
                "div > div.col > div.text-truncate > a > strong"));
        //获取帖子标题
        String title = detailsElement.getText();

        //获取作者名称
        String name = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col > ul > li:nth-child(1)")).getText();
        //获取发布时间
        String time = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)")).getText();
        //获取浏览量
        String pageView = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col >" +
                " div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)")).getText();
        //获取点赞数
        String like = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)")).getText();
        //获取回复数
        String reply = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > " +
                "div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")).getText();

        //进入帖子详情页
        detailsElement.click();
        //使用显示等待
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#article_details_reply")));


        //1、可以正确加载帖子的内容
        //标题是否一致（有两处有标题）
        String title1 = driver.findElement(By.xpath("//*[@id=\"details_article_title\"]")).getText();
        Assertions.assertEquals(title, title1);
        String title2 = driver.findElement(By.xpath("//*[@id=\"details_article_content_title\"]")).getText();
        Assertions.assertEquals(title, title2);

        //帖子发布时间(以及图标)
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > " +
                "div.col-auto.d-none.d-md-inline > ul > li:nth-child(1) > svg"));
        String time1 = driver.findElement(By.cssSelector("#details_article_createTime")).getText();
        Assertions.assertEquals(time, time1);

        //帖子浏览量(以及图标)
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > " +
                "div.col-auto.d-none.d-md-inline > ul > li:nth-child(2) > svg"));
        String pageView1 = driver.findElement(By.cssSelector("#details_article_visitCount")).getText();
        int pv = Utils.transition(pageView);
        int pv2 = Utils.transition(pageView1);
        Assertions.assertEquals(pv + 1, pv2);

        //帖子点赞数(以及图标)
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > " +
                "div.col-auto.d-none.d-md-inline > ul > li:nth-child(3) > svg"));
        String like1 = driver.findElement(By.cssSelector("#details_article_likeCount")).getText();
        Assertions.assertEquals(like, like1);

        //帖子回复数(以及图标)
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div >" +
                " div.col-auto.d-none.d-md-inline > ul > li:nth-child(4) > svg"));
        String reply1 = driver.findElement(By.cssSelector("#details_article_replyCount")).getText();
        Assertions.assertEquals(reply, reply1);

        //作者头像
        driver.findElement(By.cssSelector("#article_details_author_avatar"));
        //作者名称
        String name1 = driver.findElement(By.cssSelector("#article_details_author_name")).getText();
        Assertions.assertEquals(name, name1);
        //帖子详情内容
        String content = driver.findElement(By.cssSelector("#details_article_content > p")).getText();
        Assertions.assertNotNull(content);

        //帖子回复模块
        String replyText = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > " +
                "div > div:nth-child(2) > h3")).getText();
        Assertions.assertEquals("最新回复", replyText);
        driver.findElement(By.cssSelector("#details_reply_area"));

        //帖子回复区
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > " +
                "div:nth-child(4) > div > div.card-body"));

        //2、非当前用户发布的帖子，可以进行点赞、回复、发私信操作
        //点赞
//        WebElement likeElement = driver.findElement(By.cssSelector("#details_btn_like_count"));
//        String likeText = likeElement.getText();
//        Assertions.assertEquals("点赞",likeText);
//        int like11 = Utils.transition(like1);
//        likeElement.click();
//        //等待点赞成功
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right" +
//                " > div > h2")));
//        //对比前后点赞数是否+1
//        String likeFastText = driver.findElement(By.cssSelector("#details_article_likeCount")).getText();
//        int likeFastText1 = Utils.transition(likeFastText);
//        Assertions.assertEquals(like11+1,likeFastText1);

        //回复(检查回复按钮是否可用)
        WebElement element = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > " +
                "div:nth-child(4) > div > div.card-footer.bg-transparent.mt-auto > div>div>div>a"));
        Actions action = new Actions(driver);
        action.click(element).build().perform();
        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s = text.split("\n");
        Assertions.assertEquals("请输入回复内容",s[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > " +
                "span.close-jq-toast-single")).click();

        //发私信
        //查看按钮是否存在
        driver.findElement(By.cssSelector("#btn_details_send_message")).click();
        //检查出现的页面是否与预期一样
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#index_message_modal > div > div > " +
                "div.modal-body > div > div > div")));
        String letter = driver.findElement(By.cssSelector("#index_message_modal > div > div > " +
                "div.modal-header > h5")).getText();
        Assertions.assertEquals("发送站内信",letter);
        String send = driver.findElement(By.cssSelector("#index_message_receive_user_name")).getText();
        Assertions.assertEquals("发送给: "+name1,send);
        String pass = driver.findElement(By.cssSelector("#index_message_receive_cancel")).getText();
        Assertions.assertEquals("取消",pass);
        WebElement okElement = driver.findElement(By.cssSelector("#btn_index_send_message"));
        String ok = okElement.getText();
        Assertions.assertEquals("发送",ok);
        driver.findElement(By.cssSelector("#index_message_receive_content")).sendKeys("哈哈哈");
        okElement.click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-" +
                "right > div")));
        String okText = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        s = okText.split("\n");
        Assertions.assertEquals("站内信发送成功",s[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > " +
                "span.close-jq-toast-single")).click();

    }

    //添加一个方法用来检查当前用户为帖子作者时多出来的功能
    public void authorDetails()  {
        //当前用户发布的帖子，可以进行点赞、编辑、删除、回复操作(主要检查按钮是否可点击以及按钮上的文字是否与预期一致)
        WebElement element = driver.findElement(By.xpath("//*[@id=\"nav_board_index\"]"));
        //将页面滚动到元素所在处
        Actions action = new Actions(driver);
        action.moveToElement(element).click().perform();

        //切换到当前用户发布的帖子
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate " +
                "> a > strong")).click();


        //编辑
        driver.findElement(By.cssSelector("#details_artile_edit")).click();
        //等待一下编辑页面的加载
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#bit-forum-content > div.page-body >" +
                " div > div > div > div > div.card-body > div:nth-child(4) > div.col-10")));

        //检查编辑页面是否与预期一样
        String compile = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header." +
                "d-print-none > div > div > div > h2")).getText();
        Assertions.assertEquals("编辑",compile);
        String title = driver.findElement(By.cssSelector("#edit_article_board_name")).getText();
        Assertions.assertTrue(title.contains("区"));
        WebElement submitElement = driver.findElement(By.cssSelector("#edit_article_submit"));
        String submit = submitElement.getText();
        Assertions.assertEquals("提交",submit);

        //点击提交按钮
        action.moveToElement(submitElement).click().perform();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap." +
                "bottom-right > div")));

        WebElement sucElement = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String submitText = sucElement.getText();
        String[] s = submitText.split("\n");
        Assertions.assertEquals("修改成功",s[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > " +
                "span.close-jq-toast-single")).click();
        //检查是否正确跳转到了首页
        String homePage = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("首页",homePage);

        //删除
        //切换回帖子页面
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate " +
                "> a > strong")).click();

        WebElement deleteElement = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > " +
                "div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto." +
                "justify-content-end > div > div:nth-child(3) > div > a"));
        String delete = deleteElement.getText();
        Assertions.assertEquals("删除",delete);
        deleteElement.click();
        //检查浮窗内容是否与预期一样
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#details_delete_modal > div > div")));
        String deleteText1 = driver.findElement(By.xpath("//*[@id=\"details_delete_modal\"]/div/div" +
                "/div[2]/h3")).getText();
        Assertions.assertEquals("确定要删除当前帖子吗?",deleteText1);
        String deleteText2 = driver.findElement(By.cssSelector("#details_delete_modal > div > div > " +
                "div.modal-body.text-center.py-4 > div")).getText();
        Assertions.assertEquals("点击确认该帖子将被删除.",deleteText2);
        String cancel = driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div" +
                " > div > div:nth-child(1) > a")).getText();
        Assertions.assertEquals("取消", cancel);
        String deleteText = driver.findElement(By.cssSelector("#details_artile_delete")).getText();
        Assertions.assertEquals("删除", deleteText);

        //点击确认删除
        driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > " +
                "div > div:nth-child(2)")).click();
        //检查是否正确跳转到了首页
        homePage = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("首页",homePage);

    }
}


